package com.huping.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.huping.pojo.Grade;
import com.huping.pojo.Homework;
import com.huping.pojo.PageBean;
import com.huping.pojo.Result;
import com.huping.service.CourseService;
import com.huping.service.HomeworkService;
import com.huping.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;
    @PostMapping("/homework/teacher-create")
    public Result teacherCreate(@RequestHeader String token,@RequestBody Homework homework){
        Claims claims= JwtUtils.parseJWT(token);
        return homeworkService.teacherCreate(homework,claims);
    }

    @PostMapping("/homework/details")
    public Result details(@RequestBody JSONObject param){
        return homeworkService.details(param);
    }

    @PostMapping ("/homework/select")
    public Result select(@RequestHeader String token , @RequestBody JSONObject param){
        Claims claims= JwtUtils.parseJWT(token);
        String kind = (String) claims.get("kind");
        PageBean pageBean;
        if (kind.equals("1")||kind.equals("2")){
            pageBean = homeworkService.homeworkSelect(claims,param);
            return Result.success(pageBean);
        }
        else
            return Result.error("未知身份");
    }

    @PostMapping("/homework/teacher-annex")
    public Result teacherAnnex(@RequestParam("homeworkId") Integer homeworkId,@RequestParam("files") List<MultipartFile> files){
        return homeworkService.teacherAnnex(homeworkId,files);
    }

    @PostMapping("/homework/teacher-update")
    public Result teacherUpdate(@RequestBody Homework homework){
        return homeworkService.teacherUpdate(homework);
    }

    @PostMapping("/homework/submit-details")
    public Result submitDetails(@RequestHeader String token, @RequestBody Grade grade){
        Claims claims= JwtUtils.parseJWT(token);
        return homeworkService.submitDetails(grade.getHomeworkId(),claims);
    }

    @PostMapping("/homework/student-update")
    public Result studentUpdate(@RequestBody Grade grade){
        return homeworkService.studentUpdate(grade);
    }

    @PostMapping("/homework/student-annex")
    public Result studentAnnex(@RequestParam("gradeId") Integer gradeId, @RequestParam("files") List<MultipartFile> files){

        return homeworkService.studentAnnex(gradeId,files);

    }

    @GetMapping("/homework/teacher-select-all-course")
    public Result teacherSelectAllCourse(@RequestHeader String token){
        Claims claims= JwtUtils.parseJWT(token);
        String userid= (String) claims.get("userid");
        return homeworkService.teacherSelectAllCourse(userid);
    }

    @PostMapping("/homework/teacher-annex-delete")
    public Result teacherAnnexDelete(@RequestBody JSONObject param){
        Integer homeworkId=param.getInteger("homeworkId");
        String url=param.getString("url");
        return homeworkService.teacherAnnexDelete(homeworkId,url);
    }

    @PostMapping("/homework/student-annex-delete")
    public Result studentAnnexDelete(@RequestBody JSONObject param){
        Integer gradeId=param.getInteger("gradeId");
        String url=param.getString("url");
        return homeworkService.studentAnnexDelete(gradeId,url);
    }

    @PostMapping("/homework/check-similarity")
    public Result checkSimilarity(@RequestBody JSONObject param){
        Integer homeworkId=param.getInteger("homeworkId");
        return homeworkService.checkSimilarity(homeworkId);
    }
   @PostMapping("/homework/teacher-get-all-homework")
    public  Result teacherGetAllHomework(@RequestHeader String token , @RequestBody JSONObject param){

       Claims claims= JwtUtils.parseJWT(token);
       String userid = (String) claims.get("userid");
       return homeworkService.teacherGetAllHomework(userid,param);

   }


}
